package com.example.hyclipboard;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.List;

public class SocketClient {
	private static final String TAG = "HOYIN_TCPClient";
	private final String server;
	private final int port;
	private final Handler handler;

	public static final int CONN_FAILED = -1;
	public static final int CONN_SUCCESS = 1;

	@SuppressLint("HandlerLeak")
	SocketClient(String server, int port, Handler handler) {
		this.server = server;
		this.port = port;
		this.handler = handler;
	}

	public void send(String data) {
		new Thread(() -> {
			StringBuilder recv = new StringBuilder();
			try {
				Socket socket = new Socket(server, port);
				if (!socket.isConnected()) {
					Message msg = Message.obtain();
					msg.what = CONN_FAILED;
				}
				OutputStream os = socket.getOutputStream();
				PrintWriter pw = new PrintWriter(os);
				pw.write(data);
				pw.flush();
				socket.shutdownOutput();

				InputStream is = socket.getInputStream();
				InputStreamReader isr = new InputStreamReader(is);
				BufferedReader br = new BufferedReader(isr);
				while (true) {
					String line = br.readLine();
					if (line == null) {
						break;
					}
					recv.append(line);
				}
				socket.shutdownInput();
				br.close();
				isr.close();
				is.close();
				pw.close();
				os.close();
				socket.close();
				Log.e(TAG, recv.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
			Message msg = Message.obtain();
			msg.what = CONN_SUCCESS;
			msg.obj = recv.toString();
			handler.sendMessage(msg);
		}).start();
	}

	private static class SignUpRequest {
		String action;
		String nickname;
		String account;
		String password;
	}

	public static class SignUpResponse {
		Boolean status;
		String uid;
		String reason;
	}

	public static String generateSignUpRequest(String nickname, String account, String password) {
		Gson gson = new Gson();
		SignUpRequest request = new SignUpRequest();
		request.action = Macros.ACTIONS.SIGNUP;
		request.account = account;
		request.password = password;
		request.nickname = nickname;
		return gson.toJson(request);
	}

	private static class LoginRequest {
		String action;
		String account;
		String password;
	}

	public static class LoginResponse {
		Boolean status;
		String uid;
		String nickname;
		String account;
		String reason;
	}


	public static String generateLoginRequest(String account, String password) {
		Gson gson = new Gson();
		LoginRequest request = new LoginRequest();
		request.action = Macros.ACTIONS.LOGIN;
		request.account = account;
		request.password = password;
		return gson.toJson(request);
	}

	private static class PushRequest {
		String action;
		String uid;
		String time;
		String device;
		String content;
	}

	public static class PushResponse {
		Boolean status;
		String uid;
		String cid;
		String reason;
	}

	public static String generatePushRequest(String uid, String time, String content, String deviceName) {
		Gson gson = new Gson();
		PushRequest request = new PushRequest();
		request.action = Macros.ACTIONS.PUSH;
		request.uid = uid;
		request.time = time;
		request.content = content;
		request.device = deviceName;
		return gson.toJson(request);
	}

	private static class QueryRequest {
		String action;
		String uid;
		String device;
	}

	public static String generateQueryRequest(String uid, String deviceName) {
		Gson gson = new Gson();
		QueryRequest request = new QueryRequest();
		request.action = Macros.ACTIONS.QUERY;
		request.uid = uid;
		request.device = deviceName;
		return gson.toJson(request);
	}

	public static class QueryResponse {
		Boolean status = false;
		String reason;
		List<SocketClient.MemoData> items;
	}

	private static class FetchRequest {
		String action;
		String cid;
	}

	public static String generateFetchRequest(String cid) {
		Gson gson = new Gson();
		FetchRequest request = new FetchRequest();
		request.action = Macros.ACTIONS.FETCH;
		request.cid = cid;
		return gson.toJson(request);
	}

	public static class FetchResponse {
		String reason;
		String action;
		Boolean status = false;
		String cid;
		String content;
		String time;
		int read;
		String device;
	}

	private static class ReadRequest {
		String action;
		String cid;
	}

	public static String generateReadRequest(String cid) {
		Gson gson = new Gson();
		ReadRequest request = new ReadRequest();
		request.action = Macros.ACTIONS.READ;
		request.cid = cid;
		return gson.toJson(request);
	}

	public static class ReadResponse {
		String action;
		String reason;
		Boolean status = false;
	}

	public static class MemoData {
		String cid;
		String time;
		String device;
		String content;
		int read;
	}
}
